package com.wyg.blog.controller.view_blog;

import com.wyg.blog.model.blog.Comment;
import com.wyg.blog.model.blog.MyComment;
import com.wyg.blog.service.blog.CommentService;
import com.wyg.blog.service.blog.MailService;
import com.wyg.blog.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/12 20:31
 */
@RestController
@RequestMapping("/api/view/comment")
public class ViewCommentController {
    @Autowired
    CommentService commentService;

    @Autowired
    MailService mailService;

    @PostMapping("/publishComment")
    @CrossOrigin
    public JsonResult<Integer> publishComment(@RequestBody Map<String, String> commentInfo) throws MessagingException {
        Comment comment = new Comment();
        comment.setId(0);
        comment.setAuthor(commentInfo.get("author"));
        comment.setAvatar(getRandomAvatar());
        comment.setBlog_id(Integer.parseInt(commentInfo.get("blogId")));
        comment.setCreate_time(new Date());
        if (isMail(commentInfo.get("email"))) {
            comment.setEmail(commentInfo.get("email"));
        } else {
            comment.setEmail("#");
        }
        comment.setState(true);
        if (commentInfo.get("parent_id").equals("")){
            comment.setParent_id(null);
        } else {
            comment.setParent_id(Integer.parseInt(commentInfo.get("parent_id")));
        }
        if (commentInfo.get("reply").equals("")){
            comment.setReply(null);
        } else {
            comment.setReply(commentInfo.get("reply"));
        }
        comment.setContent(commentInfo.get("content"));
        comment.setIpAddress(commentInfo.get("ip"));
        boolean res = commentService.insertComment(comment);
        if (commentInfo.get("parent_id").equals("")){
            comment.setParent_id(null);
            String content = "风在哪您好,您的个人博客收到一条留言板评论,<a style=\"text-decoration:none\" href=\"https://www.wygandwdn.cn/article/" +
                    commentInfo.get("blogId") + "\">点击前往查看</a>" +
                    ",感谢捧场!<br><div><p>祝您生活愉快,万事如意!</p></div>";
            mailService.sendMessage("1973017714@qq.com", content);
        } else {
            String email = commentService.selectEmail(Integer.parseInt(commentInfo.get("parent_id")));
            if (isMail(email)) {
                comment.setParent_id(Integer.parseInt(commentInfo.get("parent_id")));
                String content = "您在风在哪个人博客博文中的评论收到回复,<a style=\"text-decoration:none\" href=\"https://www.wygandwdn.cn/article/"+
                        commentInfo.get("blogId") +"\">点击前往查看</a>" +
                        ",感谢捧场!<br><div><p>祝您生活愉快,万事如意!</p></div>";

                mailService.sendMessage(email, content);
            }
        }
        if(res) {
            return new JsonResult<>(1,"200","评论成功!");
        } else {
            return new JsonResult<>(0,"1003","评论失败!");
        }
    }

    @GetMapping("/getComments")
    @CrossOrigin
    public JsonResult<List<MyComment<Comment>>> getComments(@RequestParam("blogId") Integer blogId){
        List<MyComment<Comment>> results = commentService.selectCommentsByBid(blogId);
        if(results != null){
            return new JsonResult<>(results,"200","评论查询成功!");
        }else {
            return new JsonResult<>(null,"1004","该博客暂无评论!");
        }
    }

    public String getRandomAvatar(){
        List<String> avatarList = new ArrayList<>();
        //添加自己的图片对应地址即可
        avatarList.add("https://gitee.com/blog-map-bed/first-bed/raw/master/img/typora-icon.png");
        Random random = new Random();
        int i = random.nextInt(avatarList.size());
        return avatarList.get(i);
    }

    public static boolean isMail(String str) {
        String regEx = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx);
        m = p.matcher(str);
        if(m.matches()) {
            return true;
        }
        return false;
    }
}
